package com.jinghangzz.shop.common.util;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 * Redis的工具类
 * 	Redis就是一个容器,是Map
 * 	set:放,
 * 	get:取
 * 	keys:*查询;
 * 	delete:删除
 * @author TeaBig
 */
@Component("redisUtil")
public class RedisUtil
{
	/**
	 * 开发人员通过此类操作Redis
	 */
	@Resource
	private RedisTemplate<String,String> redisTemplate ; 
	
	/**
	 * 设置值
	 * @return
	 */
	public boolean set(String key,String value)
	{
		return this.redisTemplate.execute(new RedisCallback<Boolean>()
		{
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/* 如何把字符串变成字节数组 */
//					byte[] keyByte = key.getBytes("UTF-8");
//					byte[] valueByte = value.getBytes("UTF-8");
					/* 序列化:serialable;
					 * 		把java对象从内存中拿到内存外面(出了java虚拟机)
					 * 		java自带的就是把对象存储为二进制;
					 * 		json
					 * 		文本(string)
					 *  */
					byte[] keyByte = RedisSerializer.string().serialize(key);
					byte[] valueByte = RedisSerializer.string().serialize(value);
					/* 放到Redis中 */
					return connection.set(keyByte, valueByte);
				} catch (Exception e)
				{
					ConstatFinalUtil.SYS_LOGGER.error("==报错了==",e);
				}
				return false ; 
			}
		});
	}
	
	/**
	 * 设置值,并且加上过期时间
	 * @param seconds 多长时间以后,自己删除
	 * @return
	 */
	public boolean set(String key,String value,long seconds)
	{
		return this.redisTemplate.execute(new RedisCallback<Boolean>()
		{
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/* 如何把字符串变成字节数组 */
//					byte[] keyByte = key.getBytes("UTF-8");
//					byte[] valueByte = value.getBytes("UTF-8");
					/* 序列化:serialable;
					 * 		把java对象从内存中拿到内存外面(出了java虚拟机)
					 * 		java自带的就是把对象存储为二进制;
					 * 		json
					 * 		文本(string)
					 *  */
					byte[] keyByte = RedisSerializer.string().serialize(key);
					byte[] valueByte = RedisSerializer.string().serialize(value);
					/* 放到Redis中 */
					return connection.setEx(keyByte, seconds,valueByte);
				} catch (Exception e)
				{
					ConstatFinalUtil.SYS_LOGGER.error("==报错了==",e);
				}
				return false ; 
			}
		});
	}
	
	/**
	 * 读取
	 * @return
	 */
	public String get(String key)
	{
		return this.redisTemplate.execute(new RedisCallback<String>()
		{
			@Override
			public String doInRedis(RedisConnection connection) throws DataAccessException
			{
				String value = "";
				try
				{
					/* 如何把字符串变成字节数组 */
//					byte[] keyByte = key.getBytes("UTF-8");
//					byte[] valueByte = value.getBytes("UTF-8");
					/* 序列化:serialable;
					 * 		把java对象从内存中拿到内存外面(出了java虚拟机)
					 * 		java自带的就是把对象存储为二进制;
					 * 		json
					 * 		文本(string)
					 *  */
					byte[] keyByte = RedisSerializer.string().serialize(key);
					/* 放到Redis中 */
					byte[] valByte = connection.get(keyByte);
					value = RedisSerializer.string().deserialize(valByte);
				} catch (Exception e)
				{
					ConstatFinalUtil.SYS_LOGGER.error("==报错了==",e);
				}
				return value ; 
			}
		});
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String delete(String key)
	{
		return this.redisTemplate.execute(new RedisCallback<String>()
		{
			@Override
			public String doInRedis(RedisConnection connection) throws DataAccessException
			{
				String value = "";
				try
				{
					/* 如何把字符串变成字节数组 */
//					byte[] keyByte = key.getBytes("UTF-8");
//					byte[] valueByte = value.getBytes("UTF-8");
					/* 序列化:serialable;
					 * 		把java对象从内存中拿到内存外面(出了java虚拟机)
					 * 		java自带的就是把对象存储为二进制;
					 * 		json
					 * 		文本(string)
					 *  */
					byte[] keyByte = RedisSerializer.string().serialize(key);
					/* 放到Redis中 */
					value = connection.del(keyByte) + "";
				} catch (Exception e)
				{
					ConstatFinalUtil.SYS_LOGGER.error("==报错了==",e);
				}
				return value ; 
			}
		});
	}
}
